﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// Simple Service Administration Tool for WinNT based systems.
// Copyright © 2010 - Silvan Gehrig
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
// 
// Author:
//  Silvan Gehrig
//
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Windows.Threading;
using SSATool.Common.Logging;
using SSATool.Common.Util;
using SSATool.Common.Util.Threading;

#endregion

namespace SSATool.UI.Common.Threading
{
    /// <summary>
    /// Represents the data synchronisation dispatcher. The invoke method
    /// will be called in order to synchronize back into the windows message
    /// queue of WPF, which created and is holding the data layer.
    /// </summary>
    public class SyncDispatcher : IDataSyncDispatcher
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly Dispatcher _dispatcherLoop;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------
      

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Initializes a new instance of the <see cref="SyncDispatcher"/> class.
        /// </summary>
        /// <param name="dispatcherLoop">Specifies the main windows dispatcher loop.</param>
        public SyncDispatcher(Dispatcher dispatcherLoop)
        {
            PreCondition.AssertNotNull(dispatcherLoop, "dispatcherLoop");

            _dispatcherLoop = dispatcherLoop;
        } 

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Each call, which may be fired in an asynchronous context, will
        /// be synchronized using this dispatcher call.
        /// </summary>
        /// <param name="action">Specifies the action to execute in the main message loop.</param>
        public void Invoke(Action action)
        {
            Action toExecute = () =>
                                   {
                                       try
                                       {
                                           action();
                                       }
                                       catch (Exception e)
                                       {
                                           Logger<SyncDispatcher>.Error(e);
                                           throw;
                                       }
                                   };

            if (_dispatcherLoop.CheckAccess())
                toExecute();
            else
                _dispatcherLoop.BeginInvoke(DispatcherPriority.Background, toExecute);
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion

       
    }
}